//
//  DetailViewCtl.m
//  IFreeAppDemo
//
//  Created by qianfeng on 15/11/6.
//  Copyright © 2015年 qianfeng. All rights reserved.
//

#import "DetailViewCtl.h"
#import "IFreeInterface.h"
#import "IFreeConnect.h"
#import "UIImageView+AFNetworking.h"
#import "LimitFreeModel.h"

@interface DetailViewCtl ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UILabel *type;
@property (weak, nonatomic) IBOutlet UILabel *subType;
@property (weak, nonatomic) IBOutlet UILabel *rating;


@property (nonatomic,strong) UIScrollView * topScrollView;
@property (nonatomic,strong) UILabel * desLb;
@property (nonatomic,strong) UIScrollView * bottomScrollView;

@end

@implementation DetailViewCtl
- (UIScrollView *)topScrollView
{
    if (_topScrollView == nil) {
        _topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(32, 230, self.view.bounds.size.width-64, 123)];
        _topScrollView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_topScrollView];
    }
    return _topScrollView;
}

- (UILabel *)desLb
{
    if (_desLb == nil) {
        _desLb = [[UILabel alloc] initWithFrame:CGRectMake(32, 360, self.view.bounds.size.width-64, 80)];
        _desLb.numberOfLines = 0;
        [self.view addSubview:_desLb];
    }
    return _desLb;
}

- (UIScrollView *)bottomScrollView
{
    if (_bottomScrollView == nil) {
        _bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(32, 450, self.view.bounds.size.width-64, 100)];
        _bottomScrollView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_bottomScrollView];
    }
    return _bottomScrollView;
}


- (IBAction)click:(id)sender {
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self loadData];
}

- (void)loadData
{
    IFreeConnect * connect = [IFreeConnect shareInstance];
    // 应用详情请求
    [connect detailURL:[NSString stringWithFormat:DetailURL,self.applicationId] result:^(id data, NSError *error) {
        if (error == nil) {
            // NSLog(@"data:%@",data);
            [self refreshTopView:data];
        }
    }];
    // 周围的人请求
    [connect aroundURL:AroundURL result:^(id data, NSError *error) {
        if (error == nil) {
            [self refreshBottomView:data];
        }
    }];
}

#pragma mark - 刷新UI
- (void)refreshBottomView:(NSMutableArray *)arr
{
    for (int i = 0; i < arr.count; i++) {
        LimitFreeModel * model = arr[i];
        
        UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20+80*i, 5, 60, 80)];
        [imgView setImageWithURL:[NSURL URLWithString:model.iconUrl]];
        [self.bottomScrollView addSubview:imgView];
        
        UILabel * lb = [[UILabel alloc] initWithFrame:CGRectMake(20+80*i, 80, 60, 20)];
        lb.adjustsFontSizeToFitWidth = YES;
        lb.text = model.name;
        [self.bottomScrollView addSubview:lb];
    }
    self.bottomScrollView.contentSize = CGSizeMake(90*arr.count,10);
}

- (void)refreshTopView:(NSDictionary *)dic
{
    [self.imageView setImageWithURL:[NSURL URLWithString:dic[@"iconUrl"]]];
    self.name.text = dic[@"name"];
    self.price.text = [NSString stringWithFormat:@"原价:￥%@",dic[@"lastPrice"]];
    self.subType.text = dic[@"categoryName"];
    self.rating.text = dic[@"starCurrent"];
    self.desLb.text = dic[@"description"];
    
    // 图片刷新
    for (int i = 0; i < [dic[@"photos"] count]; i++) {
        UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20+80*i, 5, 60, 100)];
        [self.topScrollView addSubview:imgView];
        
        [imgView setImageWithURL:[NSURL URLWithString:dic[@"photos"][i][@"smallUrl"]]];
    }
    self.topScrollView.contentSize = CGSizeMake([dic[@"photos"] count]*90, 10);
}

@end
